//封装倒计时逻辑函数
import { ref, computed, onUnmounted } from "vue"
//引入dayjs
import dayjs from "dayjs"
//倒计时方法
export const useCountDown = () => {
    ///1.设立一个响应式数据
    const time = ref(0)
    let timer = null
    const formaTime = computed(() => dayjs.unix(time.value).format('mm分ss秒'))
    //2.执行倒计时逻辑 每秒运行
    const start = (currentTime) => {
        time.value = currentTime
        timer = setInterval(() => {
            time.value--
        }, 1000);
    }
    //组件销毁时清除定时器
    onUnmounted(() => {
        timer && clearInterval(timer)
    })
    return {
        formaTime,
        start
    }
}